<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <br />
        <el-card>
            <el-row>
                <el-col :span="2">
                    <el-button type="primary" @click="addFormDialog=true">添加角色</el-button>
                </el-col>
                <el-col :span="12"></el-col>
            </el-row>
            <el-table :data="roleList" border stripe>
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-row
                            :class="['bdbottom',i1===0?'bdtop':'','vcenter']"
                            v-for="(item1,i1) in scope.row.children"
                            :key="item1.id"
                        >
                            <!-- 一级权限 -->
                            <el-col :span="5">
                                <el-tag
                                    closable
                                    @close="removeRightById(scope.row,item1.id)"
                                >{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 二级、三级权限 -->
                            <el-col :span="19">
                                <el-row
                                    :class="[i2===0?'':'bdtop','vcenter']"
                                    v-for="(item2,i2) in item1.children"
                                    :key="item2.id"
                                >
                                    <el-col :span="6">
                                        <el-tag
                                            type="success"
                                            closable
                                            @close="removeRightById(scope.row,item2.id)"
                                        >{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag
                                            @close="removeRightById(scope.row,item3.id)"
                                            type="warning"
                                            closable
                                            v-for="item3 in item2.children"
                                            :key="item3.id"
                                        >{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" width="250px">
                    <template slot-scope="scope">
                        <el-row>
                            <el-tooltip content="修改" placement="top" effect="dark">
                                <el-button
                                    title="修改"
                                    type="primary"
                                    size="small"
                                    icon="el-icon-edit"
                                    @click="showEditRoles(scope.row.id)"
                                ></el-button>
                            </el-tooltip>
                            <el-tooltip content="分配权限" placement="top" effect="dark">
                                <el-button
                                    type="success"
                                    size="small"
                                    icon="el-icon-setting"
                                    @click="showSetRightDialog(scope.row)"
                                ></el-button>
                            </el-tooltip>
                            <el-tooltip content="删除" placement="top" effect="dark">
                                <el-button
                                    type="danger"
                                    size="small"
                                    icon="el-icon-delete"
                                    @click="deleteRoleById(scope.row.id)"
                                ></el-button>
                            </el-tooltip>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 添加角色对话框 -->
        <el-dialog title="添加角色" :visible.sync="addFormDialog" width="50%" @close="addFormClosed">
            <el-form :model="addForm" ref="addFormRef" :rules="addFormRules" label-width="80px">
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="addForm.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                    <el-input v-model="addForm.roleDesc"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="addFormDialog = false">取 消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 修改用户对话框 -->
        <el-dialog
            title="修改用户"
            :visible.sync="editFormDialog"
            width="50%"
            @close="editDialogClosed"
        >
            <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="80px">
                <el-form-item label="ID">
                    <el-input v-model="editForm.roleId" disabled></el-input>
                </el-form-item>
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="editForm.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                    <el-input v-model="editForm.roleDesc"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editFormDialog = false">取 消</el-button>
                <el-button type="primary" @click="editRoles">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 分配权限对话框 -->
        <el-dialog title="分配权限" :visible.sync="SetRightDialog" @close="setRightClose">
            <el-tree
                :data="rightList"
                :props="treeProps"
                show-checkbox
                node-key="id"
                default-expand-all
                :default-checked-keys="defKeys"
                ref="treeRef"
            ></el-tree>

            <span slot="footer">
                <el-button @click="SetRightDialog = false">取 消</el-button>
                <el-button type="primary" @click="allotRights">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            roleList: [],
            SetRightDialog: false,
            rightList: [],
            treeProps: {
                label: "authName",
                children: "children"
            },
            addForm: {
                roleName: "",
                roleDesc: ""
            },
            editForm: {},
            addFormRules: {
                roleName: [
                    {
                        required: true,
                        message: "请输入角色名称",
                        trigger: "blur"
                    }
                ]
            },
            editFormRules: {
                roleName: [
                    {
                        required: true,
                        message: "请输入角色名称",
                        trigger: "blur"
                    }
                ]
            },
            addFormDialog: false,
            editFormDialog: false,
            defKeys: [],
            roleId: ""
        };
    },
    created() {
        this.getRoleList();
    },
    methods: {
        getRoleList() {
            this.$axios.get("roles").then(response => {
                let res = response.data;
                if (res.meta.status !== 200)
                    return this.$message({
                        message: "获取角色列表失败",
                        type: "error",
                        showClose: true
                    });
                this.roleList = res.data;
            });
        },
        removeRightById(role, rightId) {
            this.$confirm("是否确认删除该权限？", "标题", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
                callback: action => {
                    if (action !== "confirm") return;
                    this.$axios
                        .delete(`roles/${role.id}/rights/${rightId}`)
                        .then(response => {
                            let res = response.data;
                            if (res.meta.status !== 200)
                                return this.$message({
                                    message: "删除权限失败！",
                                    type: "error",
                                    showClose: true
                                });
                            this.$message({
                                message: "删除权限成功!",
                                type: "success",
                                showClose: true
                            });
                            role.children = res.data;
                        });
                }
            });
        },
        showSetRightDialog(role) {
            this.roleId = role.id;
            this.$axios.get("rights/tree").then(response => {
                let res = response.data;
                if (res.meta.status !== 200)
                    return this.$message({
                        message: "获取权限失败！",
                        type: "error",
                        showClose: true
                    });
                this.rightList = res.data;
                this.getDeafKeys(role, this.defKeys);
            });
            this.SetRightDialog = true;
        },
        getDeafKeys(node, arr) {
            if (!node.children) return arr.push(node.id);
            node.children.forEach(element => {
                this.getDeafKeys(element, arr);
            });
        },
        setRightClose() {
            this.defKeys = [];
        },
        allotRights() {
            const key = [
                ...this.$refs.treeRef.getCheckedKeys(),
                ...this.$refs.treeRef.getHalfCheckedKeys()
            ];
            const idStr = key.join(",");
            this.$axios
                .post(`roles/${this.roleId}/rights`, { rids: idStr })
                .then(response => {
                    let res = response.data;
                    if (res.meta.status !== 200)
                        return this.$message({
                            message: "更新权限失败！",
                            type: "error",
                            showClose: true
                        });
                    this.$message({
                        message: "更新权限成功！",
                        type: "success",
                        showClose: true
                    });
                    this.getRoleList();
                    this.SetRightDialog = false;
                });
        },
        addUser() {
            this.$refs.addFormRef.validate(valid => {
                if (!valid) return;
                this.$axios.post("roles", this.addForm).then(response => {
                    let res = response.data;
                    if (res.meta.status !== 201)
                        return this.$message({
                            message: "添加角色失败！",
                            type: "error",
                            showClose: true
                        });
                    this.$message({
                        message: "添加用户成功！",
                        type: "success",
                        showClose: true
                    });
                    this.addFormDialog = false;
                    this.getRoleList();
                });
            });
        },
        addFormClosed() {
            this.$refs.addFormRef.resetFields();
        },
        deleteRoleById(id) {
            this.$confirm("是否确认删除该角色？", "删除角色", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
                callback: action => {
                    if (action !== "confirm") return;
                    this.$axios.delete(`roles/${id}`).then(response => {
                        let res = response.data;
                        if (res.meta.status !== 200)
                            return this.$message({
                                message: "删除角色失败！",
                                type: "error",
                                showClose: true
                            });
                        this.$message({
                            message: "删除用户成功！",
                            type: "success",
                            showClose: true
                        });
                        this.getRoleList();
                    });
                }
            });
        },
        showEditRoles(id) {
            this.$axios.get(`roles/${id}`).then(response => {
                let res = response.data;
                if (res.meta.status !== 200)
                    return this.$message({
                        message: "获取角色信息失败！",
                        type: "error",
                        showClose: true
                    });
                this.editForm = res.data;
                this.editFormDialog = true;
            });
        },
        editDialogClosed() {
            this.$refs.editFormRef.resetFields();
        },
        editRoles() {
            this.$refs.editFormRef.validate(valid => {
                if (!valid) return;
                this.$axios
                    .put(`roles/${this.editForm.roleId}`,this.editForm)
                    .then(response => {
                        let res = response.data;
                        console.log(res);
                        if (res.meta.status !== 200)
                            return this.$message({
                                message: "更新角色信息失败！",
                                type: "error",
                                showClose: true
                            });
                        this.$message({
                            message: "更新角色信息成功！",
                            type: "success",
                            showClose: true
                        });
                        this.editFormDialog = false;
                        this.getRoleList();
                    });
            });
        }
    }
};
</script>
<style lang="less" scoped>
.el-tag {
    margin: 10px;
}
.bdtop {
    border-top: 1px solid #eee;
}
.bdbottom {
    border-bottom: 1px solid #eee;
}
.vcenter {
    display: flex;
    align-items: center;
}
</style>